import { _decorator, Component, Node, tween, v3 } from 'cc';
import { AdComponent } from '../AdComponent';
import DatasSpace from '../Tool/DatasSpace';
import { Home } from '../Home';
import { AudioMgr, ENUM_CLIP } from '../Tool/AudioMgr';
const { ccclass, property } = _decorator;

@ccclass('GoldAddAndFtAdd')
export class GoldAddAndFtAdd extends Component {
  @property(Node)
  viewNode: Node = null;

  btnClose() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    this.btnCloseSetting();
  }

  start() {
    // AdComponent.Instance.show5X5CustomAd();
    AdComponent.Instance.showInterstitialFullShow();
    AdComponent.Instance.showBannerAd();
    // tween(this.viewNode).to(0.3, { position:v3(this.viewNode.getPosition().x,-50,0), scale:v3(1.2,0.8,0)}, { easing: 'smooth' }).to(0.3, { position: v3(0,0,0), scale:v3(1,1,0) }, { easing: 'backInOut' }).to(0.3, { scale:v3(1.1,0.9,0)}, { easing: 'backInOut' }).to(0.1, { scale:v3(1,1,0) }, { easing: 'backInOut' }).call(() => {
    // }).start();
  }

  protected onDestroy(): void {
    AdComponent.Instance.hideBannerAd();
  }

  btnCloseSetting() {
    // AdComponent.Instance.instance.playAudio(2);
    // AdComponent.Instance.hide5X5customAd();
    tween(this.viewNode).to(0.5, { position: v3(0, this.viewNode.getPosition().y + 1600, 0) }, { easing: 'backInOut' }).call(() => {
      this.node.removeFromParent();
      this.node.destroy();

    }).start();
  }

  /**初始化显示节点 */
  initViewShow(type) {
    if (type == 1) {
      this.viewNode.getChildByName('GoldNode').active = true;
    } else if (type == 2) {
      this.viewNode.getChildByName('FtNode').active = true;
    }
  }

  /**广告增加金币
   * time 2024 5 29 1:08
   */
  btnVideoAddGold() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    AdComponent.Instance.loadVideoAd(0, (res) => {
      if (res) {
        console.log('广告购买成功');
        DatasSpace.Instance.goldNum += 100;
        DatasSpace.Instance.setGoldNum();
        Home.inst.checkUserGoldNumAndFtNum();
        this.btnCloseSetting();
      }
    });
  }

  /**
   * 广告增加体力
   * time 2024 5 29 1:16
   */
  btnVideoAddFt() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    AdComponent.Instance.loadVideoAd(0, (res) => {
      if (res) {
        console.log('广告购买成功');
        DatasSpace.Instance.ftNum += 3;
        DatasSpace.Instance.setFtNum();
        Home.inst.checkUserGoldNumAndFtNum();
        this.btnCloseSetting();
      }
    });
  }

  /**
   * 金币购买体力
   * time 2024 5 29 1:22
   */
  btnAddFtByGold() {
    AudioMgr.Instance.play(ENUM_CLIP.Click);

    let ftGoldPrice: number = 100;
    if (DatasSpace.Instance.goldNum < ftGoldPrice) {
      console.log("金币不足");
      return;
    } else {
      DatasSpace.Instance.goldNum -= ftGoldPrice;
      DatasSpace.Instance.setGoldNum();
      DatasSpace.Instance.ftNum += 1;
      DatasSpace.Instance.setFtNum();
      Home.inst.checkUserGoldNumAndFtNum();
      this.btnCloseSetting();
    }
  }

  // update(deltaTime: number) {

  // }
}


